package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.FuelPriceEffectiveQueryConditionsVO;
import com.zhiche.lisa.bms.pojo.vo.driver.FuelPriceEffectiveVO;
import com.zhiche.lisa.bms.service.driver.IFuelPriceEffectiveService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 燃油价格生效表 前端控制器
 * </p>
 *
 * @author zhaoguixin
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/fuel_price")
@Slf4j
@Api(description = "燃油价格", tags = {"燃油价格"})
public class FuelPriceEffectiveController {

    @Autowired
    private IFuelPriceEffectiveService fuelPriceEffectiveService;

    @PostMapping(value = "/page_list")
    @ApiOperation(value = "分页获得燃油价格", notes = "分页获得燃油价格",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FuelPriceEffectiveVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<FuelPriceEffectiveVO>> listByPage(@ApiParam(name = "FuelPriceEffectiveVO", value = "燃油价格VO", required = true) @RequestBody Page<FuelPriceEffectiveVO> page) {
        log.info("FuelPriceEffectiveController.listByPage params : {}", page);
        RestfulResponse<Page<FuelPriceEffectiveVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<FuelPriceEffectiveVO> pageVo = fuelPriceEffectiveService.listByPage(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @GetMapping(value = "/list")
    @ApiOperation(value = "列表展示燃油价格", notes = "列表展示燃油价格",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FuelPriceEffectiveVO.class), @ApiResponse(code = -1, message = "系统异常")})
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "油价变动名称"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "fuelName", value = "燃油名称"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "fuelTypeId", value = "燃油类型主键"),
            @ApiImplicitParam(paramType = "query", dataType = "Date", name = "effectiveDate", value = "生效时间"),
            @ApiImplicitParam(paramType = "query", dataType = "Date", name = "invalidDate", value = "失效时间")
    })
    public RestfulResponse<List<FuelPriceEffectiveVO>> list(FuelPriceEffectiveQueryConditionsVO conditions) {
        log.info("FuelPriceEffectiveController.list param : {}", conditions);
        RestfulResponse<List<FuelPriceEffectiveVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(fuelPriceEffectiveService.selectListByConditions(conditions));
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.list ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.list ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/add")
    @ApiOperation(value = "添加燃油价格", notes = "添加燃油价格",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Integer> add(@ApiParam(name = "FuelPriceEffectiveVO", value = "燃油价格VO", required = true) @RequestBody @Valid FuelPriceEffectiveVO fuelPriceEffectiveVO, BindingResult br) {
        log.info("FuelPriceEffectiveController.list add : {}", fuelPriceEffectiveVO);
        RestfulResponse<Integer> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(fuelPriceEffectiveService.add(fuelPriceEffectiveVO, br));
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.list ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.list ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @GetMapping(value = "/getById")
    @ApiOperation(value = "根据ID得到油价变动信息", notes = "根据ID得到油价变动信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "油价变动Id")})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FuelPriceEffectiveVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<FuelPriceEffectiveVO> getById(Integer id) {
        log.info("DFuelPriceEffectiveController.getById param : {}", id);
        RestfulResponse<FuelPriceEffectiveVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(fuelPriceEffectiveService.getById(id));
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.getById ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.getById ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/update")
    @ApiOperation(value = "修改燃油价格", notes = "修改燃油价格",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> update(@ApiParam(name = "FuelPriceEffectiveVO", value = "燃油价格VO", required = true) @RequestBody @Valid FuelPriceEffectiveVO fuelPriceEffectiveVO, BindingResult br) {
        log.info("FuelPriceEffectiveController.update update : {}", fuelPriceEffectiveVO);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = fuelPriceEffectiveService.update(fuelPriceEffectiveVO, br);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.update ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.update ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/del")
    @ApiOperation(value = "根据ID得到删除油价变动信息", notes = "根据ID得到删除油价变动信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "油价变动Id")})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> del(Integer id) {
        log.info("FuelPriceEffectiveController.del update : {}", id);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = fuelPriceEffectiveService.del(id);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.update ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.update ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

}

